package com.itheima.tliasproject.controller;


import com.itheima.tliasproject.pojo.Clazz;
import com.itheima.tliasproject.pojo.ClazzQueryParam;
import com.itheima.tliasproject.pojo.PageResult;
import com.itheima.tliasproject.pojo.Result;
import com.itheima.tliasproject.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class ClazzController {


    @Autowired
    private ClazzService clazzService;

    //分页查询班级
    @GetMapping("/clazzs")
    public Result getAllClazz(ClazzQueryParam clazzQueryParam){
        log.info("接收到的分页参数是 {}，{}",clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        log.info("其他数据是：{}",clazzQueryParam);
       PageResult pageResult =  clazzService.getAllclass(clazzQueryParam);
        return Result.Success(pageResult);
    }

    //新增班级
    @PostMapping("/clazzs")
    public Result Save(@RequestBody Clazz clazz){
        log.info("接收到的数据是：{}",clazz);
        clazzService.updateclazz(clazz);
        return Result.Success();
    }

    //删除班级
    @DeleteMapping("/clazzs/{id}")
    public Result deleteclazz(@PathVariable Integer id){
        log.info("接收到的删除班级id是：{}",id);
        clazzService.deleteclazz(id);
        return Result.Success();
    }

    //修改班级 先查询回显数据
    @GetMapping("/clazzs/{id}")
    public Result SelectById(@PathVariable Integer id){
        log.info("查询的班级id是：{}",id);
        Clazz clazz = clazzService.selectById(id);
        return Result.Success(clazz);
    }

    @PutMapping("/clazzs")
    public Result UpdateClazz(@RequestBody Clazz clazz){
        log.info("接收到的数据是：{}",clazz);
        clazzService.update(clazz);
        return Result.Success();
    }

    //查询所有班级
    @GetMapping("/clazzs/list")
    public Result getAll(){
        List<Clazz> Clazzslist = clazzService.list();
        return Result.Success(Clazzslist);
    }
}
